﻿using System;
using System.Configuration;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Net.Mail;
using System.Collections;
namespace WinnerCMS.Common
{
    /// <summary>
    /// 发送邮件
    /// </summary>
    public class EMail 
    {
        #region 使用微软类库的发送电邮函数
        /// <summary>
        /// 发送电邮 
        /// </summary>
        /// <param name="subject"></param>
        /// <param name="body"></param>
        /// <param name="to"></param>
        /// <returns></returns>
        public bool Send(string subject, string body, string to)
        {
            return Send(subject, body, to, ConfigurationManager.AppSettings["MailFrom"], ConfigurationManager.AppSettings["MailFromName"], ConfigurationManager.AppSettings["MailDomain"], ConfigurationManager.AppSettings["MailServerUserName"], ConfigurationManager.AppSettings["MailServerPassWord"]);

        }
        /// <summary>
        /// 发送邮件
        /// </summary>
        /// <param name="subject"></param>
        /// <param name="body"></param>
        /// <param name="to"></param>
        /// <param name="mailFrom"></param>
        /// <param name="mailFromName"></param>
        /// <param name="mailDomain"></param>
        /// <param name="mailServerUserName"></param>
        /// <param name="mailServerPassWord"></param>
        /// <returns></returns>
        public bool Send(string subject, string body, string to, string mailFrom, string mailFromName, string mailDomain, string mailServerUserName, string mailServerPassWord)
        {
            try
            {
                using (MailMessage msg = new MailMessage())
                {
                    msg.From = new MailAddress(mailFrom, mailFromName);
                    msg.To.Add(new MailAddress(to, to));

                    msg.Subject = subject;
                    msg.Body = body;
                    msg.IsBodyHtml = true;
                    msg.Priority = MailPriority.Normal;
                    msg.SubjectEncoding = Encoding.UTF8;
                    msg.BodyEncoding = Encoding.UTF8;
                    msg.Headers.Add("X-Priority", "3");
                    msg.Headers.Add("X-MSMail-Priority", "Normal");
                    msg.Headers.Add("X-Mailer", "Microsoft Outlook Express 6.00.2900.2869");
                    msg.Headers.Add("X-MimeOLE", "Produced By Microsoft MimeOLE V6.00.2900.2869");
                    msg.Headers.Add("ReturnReceipt", "1");

                    using (SmtpClient client = new SmtpClient(mailDomain))
                    {
                        client.UseDefaultCredentials = false;
                        client.Credentials = new NetworkCredential(mailServerUserName, mailServerPassWord); //帐号密码
                        client.DeliveryMethod = SmtpDeliveryMethod.Network;

                        client.Send(msg);
                    }
                }
                return true;
            }
            catch { return false; }
        }
        #endregion


    }
}